package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsQuickExitDetailed;
import com.jy.wms.pojo.WmsStockMoveDetailed;
import com.jy.wms.service.WmsQuickExitDetailedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsQuickExitDetailedController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Liyi
 * @Date Sat Apr 28 09:11:38 CST 2018
 */
@RestController
@RequestMapping("/wmsquickexitdetailed")
@Api(value = "/wmsquickexitdetailed", description = "快速出库明细")
public class  WmsQuickExitDetailedController extends BaseController<WmsQuickExitDetailedService,WmsQuickExitDetailed> {

    @PostMapping(value = "/add")
    @ApiOperation(value = "快速出库 新增快速出库明细信息",response = JSONObject.class, notes = "新增快速出库明细信息")
    public @ResponseBody
    JSONObject add(HttpSession session, @RequestBody List<WmsQuickExitDetailed> entity){
        try {
            baseService.baseInsert(entity,session);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增快速出库信息明细成功","", "");
        }   catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增快速出库信息明细成功","",  "");
        } catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增快速出库信息明细失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增快速出库信息明细失败", e.getMessage(),"");
        }
    }

    @PostMapping(value = "/del")
    @ApiOperation(value = "快速出库 删除快速出库明细信息",response = JSONObject.class, notes = "删除快速出库明细信息")
    public @ResponseBody
    JSONObject del(HttpSession session, @RequestBody SetAndModifyModel setAndModifyModel){
        try {
            baseService.baseDeleteBathPrimaryKeys(setAndModifyModel.getIds());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"删除快速出库信息明细成功","", "");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"删除快速出库信息明细成功","",  "");
        } catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"删除快速出库信息明细失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"删除快速出库信息明细失败", e.getMessage(),"");
        }
    }

    @PostMapping(value = "/list/{ksckid}")
    @ApiOperation(value = "快速出库 根据ksckid查询快速出库明细信息",response = JSONObject.class, notes = "根据ksckid查询快速出库明细信息")
    public @ResponseBody
    JSONObject list(@PathVariable String ksckid){
        JSONObject json = getJSONObject();
        try {
            List<Map<String,Object>> list =baseService.getList(ksckid);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),list.size());
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询可移动库存货品
     * @param wmsStockMoveDetailed
     * @return
     */
    @PostMapping(value = "/searchKc")
    @ApiOperation(value = "快速出库 查询可移动库存货品",response = JSONObject.class, notes = "查询可移动库存货品")
    public @ResponseBody JSONObject searchKc(@RequestBody WmsStockMoveDetailed wmsStockMoveDetailed, HttpSession session) {
        JSONObject jsonObject = getJSONObject();
        Page<WmsStockMoveDetailed> list = new Page<WmsStockMoveDetailed>();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckId= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String hzId=null;
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0) {
                wmsStockMoveDetailed.setCkid(ckId);
                wmsStockMoveDetailed.setHzid(hz.get(0).getHzid());
                list = baseService.searchKc(wmsStockMoveDetailed);
            }
            jsonObject = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }


}
